What is graphql?
The graphql npm package is the JavaScript reference implementation for GraphQL, a query language for APIs and a runtime for executing those queries by using a type system you define for your data. It allows you to create schemas, define resolvers, and execute GraphQL queries and mutations.
What are graphql's main functionalities?
Creating a GraphQL Schema
This feature allows you to define the shape of your data and the way it can be queried by clients. The code sample demonstrates how to create a simple GraphQL schema with a single root query.
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'Hello world!';
}
}
}
})
});
Executing a GraphQL Query
This feature allows you to execute GraphQL queries against your schema. The code sample shows how to execute a simple query that asks for the 'hello' field, which resolves to 'Hello world!'.
const { graphql, buildSchema } = require('graphql');
const schema = buildSchema(`
type Query {
hello: String
}
`);
const root = { hello: () => 'Hello world!' };
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});
Defining Resolvers
Resolvers are functions that resolve the value for a type or field in a schema. The code sample shows how to define a resolver for the 'hello' field that returns a string.
const { GraphQLObjectType, GraphQLString } = require('graphql');
const RootQueryType = new GraphQLObjectType({
name: 'RootQuery',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'Hello world!';
}
}
}
});
Other packages similar to graphql
apollo-server
Apollo Server is a community-driven, open-source GraphQL server that works with many Node.js HTTP server frameworks. It simplifies building a GraphQL API by providing features like performance tracing and schema stitching. Compared to graphql, Apollo Server offers a more integrated set of tools for building production-ready GraphQL services.
express-graphql
Express GraphQL is a GraphQL HTTP server middleware for Express.js. It allows you to serve a GraphQL API as a web service. While graphql provides the core functionality for schema creation and query execution, express-graphql makes it easy to integrate GraphQL with the Express web application framework.
type-graphql
TypeGraphQL is a framework for building GraphQL APIs in Node.js using TypeScript, based on the official graphql-js package. It allows for defining the schema using TypeScript classes and decorators, which can be more familiar to developers with an object-oriented background. It provides a more type-safe and class-based approach to defining GraphQL schemas compared to the more traditional, function-based approach of graphql.
GraphQL.js
The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.
See more complete documentation at https://graphql.org/ and
https://graphql.org/graphql-js/.
Looking for help? Find resources from the community.
Getting Started
A general overview of GraphQL is available in the
README for the
Specification for GraphQL. That overview
describes a simple set of GraphQL examples that exist as tests
in this repository. A good way to get started with this repository is to walk
through that README and the corresponding tests in parallel.
Using GraphQL.js
Install GraphQL.js from npm
With npm:
npm install --save graphql
or using yarn:
yarn add graphql
GraphQL.js provides two important capabilities: building a type schema and
serving queries against that type schema.
First, build a GraphQL type schema which maps to your codebase.
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve() {
return 'world';
},
},
},
}),
});
This defines a simple schema, with one type and one field, that resolves
to a fixed value. The resolve
function can return a value, a promise,
or an array of promises. A more complex example is included in the top-level tests directory.
Then, serve the result of a query against that type schema.
var query = '{ hello }';
graphql(schema, query).then((result) => {
console.log(result);
});
This runs a query fetching the one field defined. The graphql
function will
first ensure the query is syntactically and semantically valid before executing
it, reporting errors otherwise.
var query = '{ BoyHowdy }';
graphql(schema, query).then((result) => {
console.log(result);
});
Note: Please don't forget to set NODE_ENV=production
if you are running a production server. It will disable some checks that can be useful during development but will significantly improve performance.
Want to ride the bleeding edge?
The npm
branch in this repository is automatically maintained to be the last
commit to master
to pass all tests, in the same form found on npm. It is
recommended to use builds deployed to npm for many reasons, but if you want to use
the latest not-yet-released version of graphql-js, you can do so by depending
directly on this branch:
npm install graphql@git://github.com/graphql/graphql-js.git#npm
Experimental features
Each release of GraphQL.js will be accompanied by an experimental release containing support for the @defer
and @stream
directive proposal. We are hoping to get community feedback on these releases before the proposal is accepted into the GraphQL specification. You can use this experimental release of GraphQL.js by adding the following to your project's package.json
file.
"graphql": "experimental-stream-defer"
Community feedback on this experimental release is much appreciated and can be provided on the issue created for this purpose.
Using in a Browser
GraphQL.js is a general-purpose library and can be used both in a Node server
and in the browser. As an example, the GraphiQL
tool is built with GraphQL.js!
Building a project using GraphQL.js with webpack or
rollup should just work and only include
the portions of the library you use. This works because GraphQL.js is distributed
with both CommonJS (require()
) and ESModule (import
) files. Ensure that any
custom build configurations look for .mjs
files!
Contributing
We actively welcome pull requests. Learn how to contribute.
Changelog
Changes are tracked as GitHub releases.
License
GraphQL.js is MIT-licensed.
Credits
The *.d.ts
files in this project are based on DefinitelyTyped definitions written by: